package com.hhit.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhit.pojo.PrivInfo;
import com.hhit.pojo.TUser;
import com.hhit.service.PrivService;
import com.hhit.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {


	@Resource
	private UserService userService;
	
	@Resource
	private PrivService privService;
	@RequestMapping("login")
	public String login(String name, String passwd,HttpSession session,ModelMap map) {
		TUser user = userService.login(name, passwd);

		if (null != user) {
			session.setAttribute("login_info", user);
			List<PrivInfo> pList =  privService.getPrivsByIden(user.getIdentify());
			map.put("pList", pList);
			return "index";
		} else {
			return "error";
		}
	}
	
	@RequestMapping("toUsers")
	public String toUsers(ModelMap map)
	{
		List<TUser> userList = userService.getAll();
		map.put("userList", userList);
		
		return "user/users";
	}
	
	@RequestMapping("del")
	public String del(Integer id)
	{
		userService.delUser(id);
		return "redirect:/user/toUsers.do";
	}
	
	@RequestMapping("add")
	public String add(TUser user)
	{
		if(user.getId() >0 )
		{
			userService.updateUser(user);
		}
		else
		{
			userService.addUser(user);
		}
		
		return "redirect:/user/toUsers.do";
	}
	
	@RequestMapping("toModify")
	public String toModify(Integer id,ModelMap map)
	{
		TUser user = userService.getUser(id);
		map.put("user", user);
		return "user/useradd";
	}
	
	@RequestMapping("checkName")
	@ResponseBody
	public boolean checkName(String name)
	{
		return userService.checkNameIsExisted(name);
	}
}
